﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QunYing.Model;

namespace Communicate
{
    public class AnalyseSms
    {
      static  QunYing.BLL.ICSA_Device bll_icsaDevice = new QunYing.BLL.ICSA_Device();
        private static char[] splitChar1 = new char[] { ';' };
        private static char[] splitChar2 = new char[] { ',' };
        public static ICSA_Device GetICSAConfig(string sms,string deviceID)
        {
            string[] subSms = sms.Split(splitChar1, StringSplitOptions.RemoveEmptyEntries);
            ICSA_Device model = bll_icsaDevice.GetModel(deviceID);
            if (model == null)
            {
                model = new ICSA_Device();
                model.ICSA设备ID = deviceID;
            }
            string[] temp = subSms[0].Split(splitChar2, StringSplitOptions.RemoveEmptyEntries);
            if (model.报警接收手机号码 != temp[1])
            {
                if (!string.IsNullOrEmpty(model.报警接收手机号码))
                {
                    QunYing.DAL.VipInfo vipInfo = new QunYing.DAL.VipInfo();
                    vipInfo.UpdataVipTele(model.报警接收手机号码, temp[1]);
                }
            }
            model.报警接收手机号码 = temp[1];
            string exterDevice = subSms[1];
            model.设备类型 = exterDevice[0] == '1' ? "全能型" : "精简型";
            model.PH传感器 = exterDevice[1] == '1';
            model.钙反应PH传感器 = exterDevice[2] == '1';
            model.ORP传感器 = exterDevice[3] == '1';
            model.温度传感器 = exterDevice[4] == '1';
            model.水位传感器 = exterDevice[5] == '1';
            model.氧循环传感器 = exterDevice[6] == '1';
            model.水循环传感器 = exterDevice[7] == '1';
            if (exterDevice[8] == '1')
                model.温度控制模式 = "加热";
            else if (exterDevice[8] == '0')
                model.温度控制模式 = "制冷";
            else
                model.温度控制模式 = "未配置";

            string[] paramConfig = subSms[2].Split(splitChar2, StringSplitOptions.RemoveEmptyEntries);
            model.PH门限值 = decimal.Parse(paramConfig[0]);
            model.钙反应器PH门限值 = decimal.Parse(paramConfig[1]);
            model.ORP门限值 = decimal.Parse(paramConfig[2]);
            model.温度门限值 = decimal.Parse(paramConfig[3]);
            model.定时器1开启时间 = new DateTime(2000, 1, 1, Int32.Parse(paramConfig[5].Substring(0, 2)), Int32.Parse(paramConfig[5].Substring(2, 2)), 0);
            model.定时器1关闭时间 = new DateTime(2000, 1, 1, Int32.Parse(paramConfig[6].Substring(0, 2)), Int32.Parse(paramConfig[6].Substring(2, 2)), 0);
            model.定时器2开启时间 = new DateTime(2000, 1, 1, Int32.Parse(paramConfig[7].Substring(0, 2)), Int32.Parse(paramConfig[7].Substring(2, 2)), 0);
            model.定时器2关闭时间 = new DateTime(2000, 1, 1, Int32.Parse(paramConfig[8].Substring(0, 2)), Int32.Parse(paramConfig[8].Substring(2, 2)), 0);
            model.报警重复间隔时间 = decimal.Parse(paramConfig[9]);
            model.开启定时上报 = paramConfig[10] == "1";
            model.定时上报时间 = new DateTime(2000, 1, 1, Int32.Parse(paramConfig[11].Substring(0, 2)), Int32.Parse(paramConfig[11].Substring(2, 2)), 0);
            return model;

        }

        public static DeviceRealTimeInfo GetRealTimeInfo(string sms,string icsaid)
        {
            string[] subSms = sms.Split(splitChar1, StringSplitOptions.RemoveEmptyEntries);
            DeviceRealTimeInfo model = new DeviceRealTimeInfo();
            string[] strInfo = subSms[2].Split(splitChar2);
            if(subSms[1].Length>15)
            switch (subSms[1][15])
            {
                case '0':
                    model.告警类型 = "定时上报";
                    break;
                case '1':
                    model.告警类型 = "环境参数异常";
                    break;
                case '2':
                    model.告警类型 = "设备故障";
                    break;
                case'3':
                    model.告警类型 = "供电异常";
                    break;
                case '4':
                    model.告警类型 = "用户请求";
                    break;
            }
            model.水族箱PH值 = decimal.Parse(strInfo[0]);
            model.钙反应器PH值 = decimal.Parse(strInfo[1]);
            model.水族箱ORP值 = decimal.Parse(strInfo[2]);
            model.水族箱温度值 = decimal.Parse(strInfo[3]);
            model.数据发送时间 = DateTime.ParseExact(strInfo[4], "yyMMddHHmm", null);
            model.蓄电池电压 = decimal.Parse(strInfo[5]);
            string[] temp = subSms[0].Split(splitChar2,StringSplitOptions.RemoveEmptyEntries);
            model.设备ID = icsaid;
            model.用户号码 = temp[1];
            QunYing.BLL.ICSA_Device icsa =new QunYing.BLL.ICSA_Device();
            var config= icsa.GetModel(model.设备ID);
            if (config == null)
                return null;
            if (config.是否已售 == null)
                return null;
            if (!config.是否已售.Value)
                return null;
            string warnInfo = subSms[3];
            
            // |1#04#8618702893493#STATUS:002800000000000,15828519435;111111110000000;06.2,15.0,+0035,00.0,1109241121,4.1;100111000000000;END#11-09-24 11:23:32
            if (config != null)
            {
                string[] dataValues = subSms[2].Split(splitChar2, StringSplitOptions.RemoveEmptyEntries);
           
                model.PH告警 = (Math.Abs((decimal.Parse(dataValues[0])) - (config.PH门限值.Value)) >= 0.2M);
                model.水温告警 = Math.Abs(decimal.Parse(dataValues[3]) - config.温度门限值.Value) >= 2M;
                model.ORP告警 = Math.Abs(decimal.Parse(dataValues[2]) - config.ORP门限值.Value) >= 50M;
                if (!config.PH传感器)
                {
                    model.PH告警 = false;
                    model.水族箱PH值 = 0;
                }
                if (!config.ORP传感器)
                {
                    model.ORP告警 = false;
                    model.水族箱ORP值 = 0;
                }
            }
            else
            {
                QunYing.Common.Loger.Log.Error(model.设备ID + "没有找到配置数据");
                model.PH告警 = warnInfo[0] == '1';
                model.ORP告警 = warnInfo[1] == '1';
                model.水温告警 = warnInfo[3] == '1';
            }
          
            model.温控设备告警 = warnInfo[2] == '1';
            model.氧循环告警 = warnInfo[4]=='1';
            model.水循环告警 = warnInfo[5] == '1';
            model.停电告警 = warnInfo[6] == '1';
            model.水位告警 = warnInfo[7] == '1';
            return model;

        }
    }
}
